function formatTime(date) {
  if (!!date) {
    if (!(date instanceof Date))
      date = new Date(date);
    var month = date.getMonth() + 1
    var day = date.getDate()
    return `${month}月${day}日`;
  }
}

function formatDay(date) {
  if (!!date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    return [year, month, day].map(formatNumber).join('-');
  }
}

function formatDay2(date) {
  if (!!date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    return [year, month, day].map(formatNumber).join('/');
  }
}

function formatWeek(date) {
  if (!!date) {
    var day = date.getDay();
    switch (day) {
      case 0:
        return '周日'
        break;
      case 1:
        return '周一'
        break;
      case 2:
        return '周二'
        break;
      case 3:
        return '周三'
        break;
      case 4:
        return '周四'
        break;
      case 5:
        return '周五'
        break;
      case 6:
        return '周六'
        break;
    }
  }
}

function formatHour(date) {
  if (!!date) {
    var hour = new Date(date).getHours();
    var minute = new Date(date).getMinutes();
    return [hour, minute].map(formatNumber).join(':');
  }
}

function timestamp(date, divisor = 1000) {
  if (date == undefined) {
    return;
  } else if (typeof date == 'number') {
    return Math.floor(date / divisor);
  } else if (typeof date == 'string') {
    var strs = date.split(/[^0-9]/);
    return Math.floor(+new Date(strs[0] || 0, (strs[1] || 0) - 1, strs[2] || 0, strs[3] || 0, strs[4] || 0, strs[5] || 0) / divisor);
  } else if (Date.prototype.isPrototypeOf(date)) {
    return Math.floor(+date / divisor);
  }
}

function detimestamp(date) {
  if (!!date) {
    return new Date(date * 1000);
  }
}

function formatNumber(n) {//给在0-9的日期加上0
  n = n.toString()
  return n[1] ? n : '0' + n
}


//比较日前大小  
function compareDate(checkStartDate, checkEndDate) {
  var arys1 = new Array();
  var arys2 = new Array();
  if (checkStartDate != null && checkEndDate != null) {
    arys1 = checkStartDate.split('-');
    var sdate = new Date(arys1[0], parseInt(arys1[1] - 1), arys1[2]);
    arys2 = checkEndDate.split('-');
    var edate = new Date(arys2[0], parseInt(arys2[1] - 1), arys2[2]);
    if (sdate > edate) {

      return false;
    } else {
      return true;
    }
  }
}
module.exports = {
  formatTime: formatTime,
  formatDay: formatDay,
  formatDay2: formatDay2,
  formatHour: formatHour,
  formatWeek: formatWeek,
  timestamp: timestamp,
  detimestamp: detimestamp,
  compareDate: compareDate
}
